package com.yfp.machine.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yfp.common.core.constant.DbConstants;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.exception.ServiceException;
import com.yfp.common.core.utils.uuid.Snowflake;
import com.yfp.common.security.utils.SecurityUtils;
import com.yfp.machine.domain.SysUser;
import com.yfp.machine.domain.TagMachine;
import com.yfp.machine.domain.vo.TagMachineVo;
import com.yfp.machine.mapper.TagMachineMapper;
import com.yfp.machine.service.MachineIsTagService;
import com.yfp.machine.service.SysUserService;
import com.yfp.machine.service.TagMachineService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;


/**
 * (TagMachine)表服务实现类
 *
 *
 * @since 2023-12-14 21:51:04
 */
@Service("tagMachineService")
public class TagMachineServiceImpl implements TagMachineService {
    @Resource
    private TagMachineMapper tagMachineMapper;

    @Resource
    private MachineIsTagService machineIsTagService;

    @Resource
    private SysUserService sysUserService;

    /**
     * 通过ID查询单条数据
     *
     * @param tagId 主键
     * @return 实例对象
     */
    @Override
    public TagMachine queryById(String tagId) {
        return this.tagMachineMapper.queryById(tagId);
    }

    /**
     * 分页查询
     *
     * @param tagMachine 筛选条件
     * @return 查询结果
     */
    @Override
    public List<TagMachine> queryAll(TagMachine tagMachine) {
        List<TagMachine> tagMachinesList = this.tagMachineMapper.queryAll(tagMachine);
        if (CollUtil.isEmpty(tagMachinesList)) return Collections.emptyList();

        for (TagMachine tagMachines : tagMachinesList) {

            if (CharSequenceUtil.isNotEmpty(tagMachines.getUpdateOper())) {
                SysUser sysUser = sysUserService.queryById(Long.valueOf(tagMachines.getUpdateOper()));

                if (ObjectUtil.isNotNull(sysUser)) {
                    tagMachines.setUpdateOper(sysUser.getNickName());
                }
            }
        }
        return tagMachinesList;
    }

    /**
     * 新增数据
     *
     * @param tagMachine 实例对象
     * @return 实例对象
     */
    @Override
    public TagMachine insert(TagMachine tagMachine) {
        this.tagMachineMapper.insert(tagMachine);
        return tagMachine;
    }

    /**
     * 修改数据
     *
     * @param tagMachine 实例对象
     * @return 实例对象
     */
    @Override
    public TagMachine update(TagMachine tagMachine) {
        int count = this.tagMachineMapper.queryTagByNameButExcludingCurrentId(tagMachine);
        if (count > 0) throw new ServiceException("标签名称重复,请核查");

        String userId = SecurityUtils.getUserId().toString();
        tagMachine.setUpdateOper(userId);

        this.tagMachineMapper.update(tagMachine);
        return this.queryById(tagMachine.getTagId());
    }

    /**
     * 通过主键删除数据
     *
     * @param tagId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String tagId) {
        return this.tagMachineMapper.deleteById(tagId) > 0;
    }

    @Override
    public TagMachine insertTagMachine(TagMachineVo tagMachineVo) {
        TagMachine tagMachine = new TagMachine();
        tagMachine.setTagName(tagMachineVo.getTagName());
        tagMachine.setCityName(tagMachineVo.getCityName());
        long count = this.tagMachineMapper.count(tagMachine);

        if (count != 0) throw new ServiceException("标签名称重复");

        Snowflake snowflake = new Snowflake(1);

        tagMachine.setTagId(String.valueOf(snowflake.nextId()));
        tagMachine.setEnable(DbConstants.machineCorrelation.ENABLE_NORMAL);
        tagMachine.setIsDelete(DbConstants.FALSE);
        String userId = SecurityUtils.getUserId().toString();
        Long deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
        tagMachine.setCreateOper(userId);
        tagMachine.setUpdateOper(userId);
        tagMachine.setDeptId(deptId);
        tagMachine.setCityName(tagMachineVo.getCityName());
        return this.tagMachineMapper.insert(tagMachine) > 0 ? tagMachine : null;

    }

    @Override
    public R<Object> disableTags(String id) {
        Long count = machineIsTagService.queryCount(id);
        if (count > 0) {
            throw new ServiceException("该标签已被绑定，请先解绑");
        }
        Long userId = SecurityUtils.getUserId();
        TagMachine tagMachine = new TagMachine();
        tagMachine.setTagId(id);
        tagMachine.setUpdateOper(userId.toString());

        TagMachine tagMachine1 = this.tagMachineMapper.queryById(id);
        if (Objects.equals(DbConstants.machineCorrelation.ENABLE_NORMAL, tagMachine1.getEnable())) {
            tagMachine.setEnable(DbConstants.machineCorrelation.DISABLED_ABNORMAL);
        } else {
            tagMachine.setEnable(DbConstants.machineCorrelation.ENABLE_NORMAL);
        }
        return this.tagMachineMapper.update(tagMachine) > 0 ? R.ok(this.queryById(id)) : R.fail("禁用失败");
    }


}
